package com.neuedu.service.impl;

import com.github.pagehelper.PageHelper;
import com.neuedu.common.JSONData;
import com.neuedu.dao.SpecsMapper;
import com.neuedu.pojo.Specs;
import com.neuedu.service.SpecsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "specsService")
public class SpecsServiceImpl implements SpecsService {
    @Autowired
    private SpecsMapper specsMapper;

    public JSONData<String> saveOrUpdateSpecs(Specs specs) {
        //-影响条数变量
        int num = 0;
        if(specs.getId()==null){//==>save
            num = specsMapper.insert(specs);
        }else{//==>update
            num = specsMapper.updateByPrimaryKey(specs);
        }
        if(num>0){
            return JSONData.buildSuccess("规格信息操作成功！");
        }else{
            return JSONData.buildError("规格信息操作失败！");
        }
    }

    public JSONData<List<Specs>> queryAllSpecs() {
        //PageHelper.startPage()==>
        List<Specs> data = specsMapper.selectAll();
        if(data==null){
            return JSONData.buildError("操作失败");
        }

        return JSONData.buildSuccess(data);
    }

    public JSONData<Specs> querySpecsById(Integer id) {
        Specs specs = specsMapper.selectByPrimaryKey(id);
        if(specs==null){
            return JSONData.buildError("操作失败");
        }
        return JSONData.buildSuccess(specs);
    }

    public JSONData<String> delSpecsById(Integer id) {
        int num = specsMapper.deleteByPrimaryKey(id);
        if(num>0){
            return JSONData.buildSuccess("操作成功！");
        }
        return JSONData.buildError("操作失败！");
    }
}
